package com.youth.service;

import com.youth.config.exception.BusinessException;
import com.youth.entity.po.User;
import com.youth.mapper.UserMapper;
import com.youth.result.ResultBean;
import com.youth.util.ShiroMd5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: wuxian
 * @date: 2020/1/6 17:30
 */
@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 根据名称获取用户信息
     *
     * @param userName
     * @return
     */
    public User getUserInfoByName(String userName) {

        if (userName != null) {
            User user = userMapper.getUserInfoByName(userName);
            return user;
        }
        return null;
    }


    /**
     * @param userName
     * @return java.util.List<java.lang.String>
     * @Description 根据名称获取角色
     * @author wuxian
     * @date 2020/1/7 9:18
     */
    public List<String> getUserRolesByName(String userName) {

        if (null != userName) {
            List<String> role = userMapper.getUserRoleByName(userName);
            return role;
        }
        return null;
    }

    /**
     * @param userName
     * @return java.util.List<java.lang.String>
     * @Description 根据名称获取权限
     * @author wuxian
     * @date 2020/1/7 9:19
     */
    public List<String> getUserPressionByName(String userName) {

        if (null != userName) {
            List<String> pression = userMapper.getUserPressionByName(userName);
            return pression;
        }
        return null;
    }

    /**
     * @param user
     * @return void
     * @Description
     * @author wuxian
     * @date 2020/1/7 10:23
     */
    public int register(User user) {
        User user1 = userMapper.getUserInfoByName(user.getUsername());
        if (null != user1) {
            throw new BusinessException("用户名已存在");
        }
        user.setPassword(ShiroMd5Util.SysMd5(user));
        int i = userMapper.insertUser(user);
        return i;
    }
}
